package org.bird.poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * @VM args : -Xms5M -Xmx5M -Xmn3M -XX:PermSize=10M -XX:MaxPermSize=10M -XX:+PrintGCDetails -XX:+UseConcMarkSweepGC -XX:+HeapDumpOnOutOfMemoryError
 * @author liangjf
 *
 */
public class HSSFReader {

	private static HSSFWorkbook workbook;

	public static void read(String path) throws FileNotFoundException, IOException {
		 workbook = new HSSFWorkbook(new FileInputStream(new File(path)));
	        HSSFSheet sheet = null;
	        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 获取每个Sheet表
	            sheet = workbook.getSheetAt(i);
	            for (int j = 0; j < sheet.getLastRowNum() + 1; j++) {// getLastRowNum，获取最后一行的行标
	                HSSFRow row = sheet.getRow(j);
	                if (row != null) {
	                    for (int k = 0; k < row.getLastCellNum(); k++) {// getLastCellNum，是获取最后一个不为空的列是第几个
	                        if (row.getCell(k) != null) { // getCell 获取单元格数据
	                            System.out.print(row.getCell(k) + "\t");
	                        } else {
	                            System.out.print("\t");
	                        }
	                    }
	                }
	                System.out.println(""); // 读完一行后换行
	            }
	            System.out.println("读取sheet表：" + workbook.getSheetName(i) + " 完成");
	        }
	}
	
	public static void main(String[] args) throws FileNotFoundException, IOException {
		URL url = HSSFReader.class.getClassLoader().getResource("reader.xls");
		HSSFReader.read(url.getPath());
	}
}
